package com.gdt.bdMessageQueue.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gdt.bdMessageQueue.mapper.ActiveMqMonitorMapper;
import com.gdt.bdMessageQueue.pojo.ActiveMqMonitor;

@Component
public class ActiveMqMonitorService {

	@Autowired
	ActiveMqMonitorMapper acMMapper;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	public String getStatus(String queueName){
		ActiveMqMonitor aqM =acMMapper.queryStatusByQueueName(queueName);
		if(aqM !=null){
			return aqM.getStatus();
		}else
			return "error";
	}
	
	public ActiveMqMonitor getActiveMqMonitorByQueueName(String queueName){
		
		return acMMapper.queryStatusByQueueName(queueName);
		
	}
	
	public ActiveMqMonitor getActiveMqMonitorBycustomerId(String customerId){
		return acMMapper.queryStatusByCustomerId(customerId);
	}
	
	public int insertValue(String queueName,String startTime,String stopTime){
		try {
			Date startServiceTime = dateFormat.parse(startTime);
			Date stopServiceTime = dateFormat.parse(stopTime);
			String id = UUID.randomUUID().toString();
			String status ="0";  //"0"代表新建的服务,还未开启
			ActiveMqMonitor aqM =new ActiveMqMonitor(id, status, queueName, startServiceTime, stopServiceTime);
			acMMapper.insertValue(aqM);
		} catch (ParseException e) {
			System.err.println("插入数据过程出错");
			e.printStackTrace();
		}
		
		
		return 1;
		
	}
	
	public void updateServiceTime(String queueName,String startTime,String stopTime){
		try {
			Date startServiceTime = dateFormat.parse(startTime);
			Date stopServiceTime = dateFormat.parse(stopTime);
			ActiveMqMonitor aqM = new ActiveMqMonitor();
			aqM.setQueueName(queueName);
			aqM.setStartServiceTime(startServiceTime);
			aqM.setStopServiceTime(stopServiceTime);
			acMMapper.updateServiceTime(aqM);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public void updateStartStatus(String queueName,String status,String customerId,Date updateTime){
		ActiveMqMonitor aqM = new ActiveMqMonitor();
		aqM.setCustomerId(customerId);
		aqM.setQueueName(queueName);
		aqM.setStatus(status);
		aqM.setUpdateTime(updateTime);
		acMMapper.updateStartStatus(aqM);
	}
	
	public void updateStopStatus(String customerId,String status,Date updateTime){
		ActiveMqMonitor aqM = new ActiveMqMonitor();
		aqM.setCustomerId(customerId);
		aqM.setStatus(status);
		acMMapper.updateStopStatus(aqM);
	}
	
	
	/**
	 * 
	 * @param time 指定日期
	 * @param from 开始日期
	 * @param to 结束日期
	 * @return
	 */
	 public  boolean isTimeIn(Date time, Date from, Date to) {
	        Calendar date = Calendar.getInstance();
	        date.setTime(time);

	        Calendar after = Calendar.getInstance();
	        after.setTime(from);

	        Calendar before = Calendar.getInstance();
	        before.setTime(to);

	        if (date.after(after) && date.before(before)) {
	            return true;
	        } else {
	            return false;
	        }
	    }
}
